<?php

namespace app\admin\controller\task;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

use app\common\model\TaskUser;

/**
 * @ControllerAnnotation(title="task_user")
 */
class User extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new TaskUser();
    }

    
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {

        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            try{
                $result = $this->model->getListInfo($where, $page, $limit, 'a.id');
                $data = [
                    'code'  => 0,
                    'msg'   => '',
                    'count' => $result['count'],
                    'data'  => $result['list'],
                ];
                return json($data);
            }catch (\Exception $e){
                _return(-1, '操作失败');
            }
        }
        return $this->fetch();
    }

    /**
     * 评价
     *
     * @return mixed
     * @throws \think\Exception
     */
    public function evaluate()
    {

        $id = input('id', '');
        $conditions = array();
        $conditions[] = array('a.id', '=', $id);
        $row = $this->model->getDetail($conditions);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {

            if($row['status'] == TaskUser::STATUS_EVALUATE){ $this->success('当前任务已评价'); }
            $post = $this->request->post();
            $score = floatval(input('score', 0));

            if($score > 10 || $score < 0){ $this->error('评分有误, 范围在0~10分'); }
            $rule = [];
            $post['deal_uid'] = session('admin.id');
            $post['status'] = TaskUser::STATUS_EVALUATE;
            $post['evaluate_time'] = date('Y-m-d H:i:s');
            $this->validate($post, $rule);
            $conditions = [];
            $conditions[] = array('id', '=', $id);
            try {
                $this->model->updateInfo($conditions, $post);
            } catch (\Exception $e) {
                echo $e->getMessage();die;
                $this->error('保存失败');
            }
            $this->success('保存成功');
        }
        $this->assign('row', $row);

        return $this->fetch();
    }

    /**
     * 详情
     *
     * @return mixed
     * @throws \think\Exception
     */
    public function detail()
    {

        $id = input('id', '');
        $conditions = array();
        $conditions[] = array('a.id', '=', $id);
        $row = $this->model->getDetail($conditions);
        empty($row) && $this->error('数据不存在');
        $this->assign('row', $row);

        return $this->fetch();
    }
}